package com.rljk.controller.backend;/**
 * Created by mac on 2018/7/11.
 */

import com.rljk.common.controller.BaseController;
import com.rljk.common.result.CodeMsg;
import com.rljk.common.result.Result;
import com.rljk.domain.common.PageData;
import com.rljk.service.TaskManager;
import com.rljk.utils.AppUtil;
import com.rljk.utils.Tools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Iterator;
import java.util.List;

/**
 * @ClassName TaskServiceController
 * @Description
 * @Author zhang
 * @Date 2018/7/11 14:00
 */
@RestController
@RequestMapping("/backendtask")
@Slf4j
public class TaskController extends BaseController {

    @Resource(name = "taskService")
    private TaskManager taskService;

    /**
     * @Author zhang
     * @Description  后台任务列表（全部）
     * @Date 2018/7/12 15:05
     * @Param []
     * @return java.lang.Object
     **/
    @RequestMapping(value="/listAll")
    @ResponseBody
    public Object taskListAll(){
        log.info("后台任务列表（全部）");
        PageData pd = new PageData();
        pd = this.getPageData();
        try{
            if(Tools.checkKey("TASKLISTALL", pd.getString("FKEY"))){	//检验请求key值是否合法
                //if(AppUtil.checkParam("taskListAll", pd)){	//检查参数
                    log.info("请求参数："+pd.toString());
                    List<PageData> listAll = taskService.backendListAll(pd);
                Iterator<PageData> it = listAll.iterator();
                while (it.hasNext()){
                    PageData next = it.next();
                    System.out.println(next.get("CREATE_TIME"));
                }
                return Result.success(listAll);
//                }else {
//                    return Result.error(CodeMsg.PARAM_LACK);
//                }
            }else{
                return Result.error(CodeMsg.FKEY_FAIL);
            }
        }catch (Exception e){
            log.error(e.toString(), e);
            return Result.error(CodeMsg.SERVER_ERROR);
        }
    }


    /**
     * @Author zhang
     * @Description  后台查看单个任务详细
     * @Date 2018/7/12 15:07
     * @Param []
     * @return java.lang.Object
     **/
    @RequestMapping(value="/taskinfo")
    @ResponseBody
    public Object taskInfo(){
        log.info("后台查看单个任务详细");
        PageData pd = new PageData();
        pd = this.getPageData();
        try{
            if(Tools.checkKey("TASKINFO", pd.getString("FKEY"))){	//检验请求key值是否合法
                if(AppUtil.checkParam("taskInfo", pd)){	//检查参数
                log.info("请求参数："+pd.toString());
                    PageData taskInfo = taskService.findById(pd);

                    return Result.success(taskInfo);
                }else {
                    return Result.error(CodeMsg.PARAM_LACK);
                }
            }else{
                return Result.error(CodeMsg.FKEY_FAIL);
            }
        }catch (Exception e){
            log.error(e.toString(), e);
            return Result.error(CodeMsg.SERVER_ERROR);
        }
    }

}
